﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace ZHJPMGR_WebApplication.Handler
{
    /// <summary>
    /// FileDownloadHandler 的摘要说明
    /// </summary>
    public class FileDownloadHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
          // string fileName = context.Request.Params["fileName"].ToString();
          // string fullName = context.Request.Params["filePath"].ToString();
           //string fullName = "C:\\uploadfile\\测试.doc";
           //string fileName = "测试.doc";
            string fileName = context.Request.QueryString["fileName"].ToString();
            string fullName = context.Request.QueryString["filePath"].ToString();
            FileInfo fileInfo = new FileInfo(fullName);
            try
            {
                context.Response.Clear();
                context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                context.Response.ContentType = "application/octet-stream";
                context.Response.WriteFile(fileInfo.FullName);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
                //context.Response.End();
                context.Response.Flush();
                context.Response.Clear();
               // context.Response.Write("0");
            }
            catch(Exception ex){
                context.Response.Write(ex.Message);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}